using System;
using System.Reflection;
using System.Runtime.Serialization;

namespace SharpCore.NullableTypes
{
	/// <summary>
	/// Wraps the <see cref="System.Byte"/> value type in an object so that references can be null.
	/// </summary>
	[Serializable]
	public sealed class NullableByte : INullable
	{
		/// <summary>
		/// The <see cref="System.Byte"/> value this instance represents.
		/// </summary>
		private byte byteValue;

		/// <summary>
		/// Initializes a new instance of the NullableByte class.
		/// </summary>
		public NullableByte()
		{
			this.byteValue = 0x00;
		}

		/// <summary>
		/// Initializes a new instance of the NullableByte class with a <see cref="System.Byte"/> value.
		/// </summary>
		/// <param name="byteValue">The <see cref="System.Byte"/> that this instance represents.</param>
		public NullableByte(byte byteValue)
		{
			this.byteValue = byteValue;
		}

		/// <summary>
		/// Initializes a new instance of the NullableByte class with a <see cref="System.String"/> that represents a <see cref="System.Byte"/>.
		/// </summary>
		/// <param name="byteValue">The string representation of a <see cref="System.Byte"/> value.</param>
		public NullableByte(string byteValue)
		{
			this.byteValue = Byte.Parse(byteValue);
		}

		/// <summary>
		/// Creates a NullableByte that is serializable with the specified SerializationInfo and StreamingContext.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableByte.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableByte.</param>
		private NullableByte(SerializationInfo info, StreamingContext context)
		{
			this.byteValue = (byte) info.GetByte("Value");
		}

		/// <summary>
		/// Returns the data needed to serialize the NullableByte.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableByte.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableByte.</param>
		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Value", byteValue);
		}

		/// <summary>
		/// Gets or Sets the <see cref="System.Byte"/> value this instance represents.
		/// </summary>
		public byte ByteValue
		{
			get { return byteValue; }
			set { byteValue = value; }
		}

		/// <summary>
		/// Converts a <see cref="System.Byte"/> to a NullableByte.
		/// </summary>
		/// <param name="byteValue">The <see cref="System.Byte"/> to be converted.</param>
		/// <returns>A NullableByte instance that wraps the <see cref="System.Byte"/> value.</returns>
		public static implicit operator NullableByte(byte byteValue)
		{
			return new NullableByte(byteValue);
		}

		/// <summary>
		/// Checks two NullableByte instances for equality.
		/// </summary>
		/// <param name="a">A NullableByte instance to test for equality.</param>
		/// <param name="b">A NullableByte instance to test for equality.</param>
		/// <returns><b>true</b> if both instances are the same; otherwise <b>false</b>.</returns>
		public static bool operator ==(NullableByte a, NullableByte b)
		{
			if (a == null && b == null)
			{
				return true;
			}
			else if (a == null || b == null)
			{
				return false;
			}
			else
			{
				return (a.ByteValue == b.ByteValue);
			}
		}

		/// <summary>
		/// Checks two NullableByte instances for inequality.
		/// </summary>
		/// <param name="a">A NullableByte instance to test for inequality.</param>
		/// <param name="b">A NullableByte instance to test for inequality.</param>
		/// <returns><b>true</b> if each instance is different; otherwise <b>false</b>.</returns>
		public static bool operator !=(NullableByte a, NullableByte b)
		{
			if (a == null && b == null)
			{
				return false;
			}
			else if (a == null || b == null)
			{
				return true;
			}
			else
			{
				return (a.ByteValue != b.ByteValue);
			}
		}

		/// <summary>
		/// Compares this instance to a specified object and returns an indication of their relative values.
		/// </summary>
		/// <param name="obj">An object to compare to this instance, or a null reference.</param>
		/// <returns>A signed integer that indicates the relative order of this instance and obj.</returns>
		public int CompareTo(object obj)
		{
			if (obj == null)
			{
				return 1;
			}
			else if (obj is NullableByte)
			{
				NullableByte nullableByte = obj as NullableByte;
				return byteValue.CompareTo(nullableByte.ByteValue);
			}
			else if (obj is byte)
			{
				return byteValue.CompareTo(obj);
			}
			else
			{
				throw new ArgumentException("The specified object was neither a byte nor a NullableByte instance.");
			}
		}

		/// <summary>
		/// Returns a value indicating whether this instance is equal to a specified object.
		/// </summary>
		/// <param name="obj">An object to compare to this instance.</param>
		/// <returns><b>true</b> if obj is a NullableByte and has the same value as this instance; otherwise, <b>false</b>.</returns>
		public override bool Equals(object obj)
		{
			return byteValue.Equals(obj);
		}

		/// <summary>
		/// Returns the hash code for this instance.
		/// </summary>
		/// <returns>A hash code for the current NullableByte.</returns>
		public override int GetHashCode()
		{
			return byteValue.GetHashCode();
		}

		/// <summary>
		/// Converts the value of this instance to its equivalent string representation.
		/// </summary>
		/// <returns>The string representation of the value of this instance.</returns>
		public override string ToString()
		{
			return byteValue.ToString();
		}
	}
}
